package sicau.car.utill;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import sicau.car.exception.ApiException;

import java.util.HashMap;

/**
 * @Author ITcz
 * @Data 2021-10-09 - 22:24
 * 对蓝卡云上的车辆预约记录 查询和删除工具类
 */
public class BlueCardUtil {
    private static Gson gson = new Gson();

    private static final String CHENGDU_SIGN = "426d2985aa0dd727dde7e3e0f09babf8";

    private static final String YAAN_SIGN = "2a1ac789a5a1bc8b1ab67d7029dc6d72";

    private static final String CHENGDU_PARKNUMBER = "p200713145437";

    private static final String YAAN_PARKNUMBER = "p200713143536";

    public static String queryYaAn(String plateNumber) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://carorder.sicau.edu.cn/bcopenapi/out/queryWhite";

        HashMap map = new HashMap<String, Object>(16);

        map.put("parkNumber", YAAN_PARKNUMBER);
        map.put("size", 1);

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("plate", plateNumber);
        JsonArray arrayData = new JsonArray();
        arrayData.add(jsonObject);
        map.put("datas", arrayData);

        HttpHeaders headers = new HttpHeaders();
        headers.add("sign", YAAN_SIGN);
        headers.add("content-type", "application/json");

        final String json = gson.toJson(map);
        final String result = restTemplate.postForObject(url, new HttpEntity<>(json, headers), String.class);
        BlueCardResDTO blueCardResDTO = gson.fromJson(result, BlueCardResDTO.class);

        if (result.contains("success")) {
            return blueCardResDTO.getDatas().toString();
        }else {
            throw new ApiException(result);
        }
    }

    public static String querychengdu(String plateNumber) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://carorder.sicau.edu.cn/bcopenapi/out/queryWhite";

        HashMap map = new HashMap<String, Object>(16);

        map.put("parkNumber", CHENGDU_PARKNUMBER);
        map.put("size", 1);

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("plate", plateNumber);
        JsonArray arrayData = new JsonArray();
        arrayData.add(jsonObject);
        map.put("datas", arrayData);

        HttpHeaders headers = new HttpHeaders();
        headers.add("sign", CHENGDU_SIGN);
        headers.add("content-type", "application/json");

        final String json = gson.toJson(map);
        final String result = restTemplate.postForObject(url, new HttpEntity<>(json, headers), String.class);
        BlueCardResDTO blueCardResDTO = gson.fromJson(result, BlueCardResDTO.class);

        if (result.contains("success")) {
            return blueCardResDTO.getDatas().toString();
        }else {
            throw new ApiException(result);
        }
    }

    public static void deleteYaAn(String plateNumber) {
        if (plateNumber == null) {
            throw new ApiException("需要删除的车牌号为空");
        }

        RestTemplate restTemplate = new RestTemplate();
        String url = "https://carorder.sicau.edu.cn/bcopenapi/out/delWhite";

        HashMap map = new HashMap<String, Object>(16);

        map.put("parkNumber", YAAN_PARKNUMBER);
        map.put("plate", plateNumber);

        HttpHeaders headers = new HttpHeaders();
        headers.add("sign", YAAN_SIGN);
        headers.add("content-type", "application/json");

        final String json = gson.toJson(map);
        final String result = restTemplate.postForObject(url, new HttpEntity<>(json, headers), String.class);

        if (result.contains("success")) {
            return;
        }else {
            throw new ApiException(result);
        }
    }

    public static void deletechengdu(String plateNumber) {
        if (plateNumber == null) {
            throw new ApiException("需要删除的车牌号为空");
        }

        RestTemplate restTemplate = new RestTemplate();
        String url = "https://carorder.sicau.edu.cn/bcopenapi/out/delWhite";

        HashMap map = new HashMap<String, Object>(16);

        map.put("parkNumber", CHENGDU_PARKNUMBER);
        map.put("plate", plateNumber);

        HttpHeaders headers = new HttpHeaders();
        headers.add("sign", CHENGDU_SIGN);
        headers.add("content-type", "application/json");

        final String json = gson.toJson(map);
        final String result = restTemplate.postForObject(url, new HttpEntity<>(json, headers), String.class);

        if (result.contains("success")) {
            return;
        }else {
            throw new ApiException(result);
        }
    }


}
